//
//  ForgetVC.m
//  Base_App
//
//  Created by Yutian Duan on 2019/5/29.
//  Copyright © 2019年 Wanwin. All rights reserved.
//

#import "ForgetVC.h"
#import "ForgetServer.h"
#import "YFBaseTextField.h"

#define TextFieldFont 13.0f

@interface ForgetVC ()
@property (nonatomic, strong) ForgetServer *forgetServer;
@property (nonatomic, strong) YFBaseTextField *phoneField;
@property (nonatomic, strong) YFBaseTextField *codeField;
@property (nonatomic, strong) UIButton *codeButton;
@property (nonatomic, strong) YFBaseTextField *passwordField;

@end

@implementation ForgetVC

- (UIButton *)codeButton {
  if (!_codeButton) {
    _codeButton = [[UIButton alloc] initWithFrame:CGRectMake(_codeField.yt_right + 10, _codeField.yt_top + 4, _codeField.yt_width - 10, _codeField.yt_height-8)];
    _codeButton.titleLabel.font = [UIFont systemFontOfSize:TextFieldFont];
    [_codeButton setTitle:YTLocalizedString(@"获取验证码") forState:UIControlStateNormal];
    [_codeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _codeButton.layer.masksToBounds = YES;
    _codeButton.layer.cornerRadius = 8;
    _codeButton.backgroundColor = HEXCOLOR(0xbb2b25);
    [_codeButton addTarget:self action:@selector(sendCode) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_codeButton];
  }
  return _codeButton;
}

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  [self setDefaltNavBar];
  [self setStatusBarStyleBack];
}

- (void)createFields {
  [super createFields];
  [self createBlackBack];
  self.navigationItem.title = YTLocalizedString(@"忘记密码");
  _forgetServer = [[ForgetServer alloc] init];
}

- (void)createViews {
  [super createViews];
  
  UILabel *stepLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, NavBarHeight + 40, ScreenWidth, 30)];
  stepLabel.text = [NSString stringWithFormat:@"%@:",YTLocalizedString(@"经以下简单步骤即可找回你的密码")];
  stepLabel.font = [UIFont systemFontOfSize:13.0f];
  stepLabel.textAlignment = NSTextAlignmentCenter;
  stepLabel.textColor = DEF_GRAY;
  [self.view addSubview:stepLabel];
  

  ///!
  UILabel *phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, stepLabel.yt_bottom + 20, 100, 35)];
  phoneLabel.text = [NSString stringWithFormat:@"%@：",YTLocalizedString(@"登记手机号码")];
  phoneLabel.textColor = [UIColor whiteColor];
  phoneLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:TextFieldFont];
  phoneLabel.textAlignment = NSTextAlignmentLeft;
  phoneLabel.textColor = [UIColor blackColor];
  [self.view addSubview:phoneLabel];
  
  
  _phoneField = [[YFBaseTextField alloc] initWithFrame:CGRectMake(phoneLabel.yt_right, phoneLabel.yt_top, self.view.yt_width - phoneLabel.yt_right - 40, phoneLabel.yt_height)];
  [_phoneField setTextFont:[UIFont systemFontOfSize:TextFieldFont]];
  [_phoneField set_placeTextColor:DEF_GRAY];
  _phoneField.placeholder = YTLocalizedString(@"输入已登记手机号码");
  _phoneField.layer.masksToBounds = YES;
  _phoneField.layer.cornerRadius = 8;
  _phoneField.backgroundColor = [UIColor whiteColor];
  _phoneField.leftViewMode = UITextFieldViewModeAlways;
  _phoneField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, _phoneField.yt_height)];
  [self.view addSubview:_phoneField];
  
  ///!
  UILabel *codeLabel = [[UILabel alloc] initWithFrame:CGRectMake(phoneLabel.yt_left, phoneLabel.yt_bottom + 20, phoneLabel.yt_width, phoneLabel.yt_height)];
  codeLabel.text = [NSString stringWithFormat:@"%@：",YTLocalizedString(@"手机验证码")];
  codeLabel.textColor = [UIColor whiteColor];
  codeLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:TextFieldFont];
  codeLabel.textAlignment = NSTextAlignmentLeft;
  codeLabel.textColor = [UIColor blackColor];
  [self.view addSubview:codeLabel];
  
  _codeField = [[YFBaseTextField alloc] initWithFrame:CGRectMake(_phoneField.yt_left, codeLabel.yt_top, _phoneField.yt_width/2, _phoneField.yt_height)];
  [_codeField setTextFont:[UIFont systemFontOfSize:TextFieldFont]];
  [_codeField set_placeTextColor:DEF_GRAY];
  _codeField.placeholder = YTLocalizedString(@"输入短讯验证码");
  _codeField.layer.masksToBounds = YES;
  _codeField.layer.cornerRadius = 8;
  _codeField.backgroundColor = [UIColor whiteColor];
  _codeField.leftViewMode = UITextFieldViewModeAlways;
  _codeField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, _phoneField.yt_height)];
  
  [self.view addSubview:_codeField];
  
  ///!
  UILabel *passwordLabel = [[UILabel alloc] initWithFrame:CGRectMake(phoneLabel.yt_left, self.codeButton.yt_bottom + 20, phoneLabel.yt_width, phoneLabel.yt_height)];
  passwordLabel.text = [NSString stringWithFormat:@"%@：",YTLocalizedString(@"新密码")];
  passwordLabel.textColor = [UIColor whiteColor];
  passwordLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:TextFieldFont];
  passwordLabel.textAlignment = NSTextAlignmentLeft;
  passwordLabel.textColor = [UIColor blackColor];
  [self.view addSubview:passwordLabel];
  
  
  _passwordField = [[YFBaseTextField alloc] initWithFrame:CGRectMake(_phoneField.yt_left, passwordLabel.yt_top, _phoneField.yt_width, _phoneField.yt_height)];
  [_passwordField setTextFont:[UIFont systemFontOfSize:TextFieldFont]];
  [_passwordField set_placeTextColor:DEF_GRAY];
  _passwordField.placeholder = [NSString stringWithFormat:@"%@",YTLocalizedString(@"输入英数组成8位以上之新密码")];
  _passwordField.layer.masksToBounds = YES;
  _passwordField.layer.cornerRadius = 8;
  _passwordField.backgroundColor = [UIColor whiteColor];
  _passwordField.leftViewMode = UITextFieldViewModeAlways;
  _passwordField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, _phoneField.yt_height)];
  [self.view addSubview:_passwordField];
  

  ///!
  UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, _passwordField.yt_bottom + 40, 148, 30)];
  [backButton setImage:[UIImage imageNamed:@"Regist_Back"] forState:UIControlStateNormal];
  backButton.yt_right = self.view.yt_width/2 - 10;
  [backButton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
  [self.view addSubview:backButton];
  
  ///!
  UIButton *submitButton = [[UIButton alloc] initWithFrame:CGRectMake(backButton.yt_right + 20, backButton.yt_top, backButton.yt_width, backButton.yt_height)];
  [submitButton setImage:[UIImage imageNamed:@"Forget_submit"] forState:UIControlStateNormal];
  [submitButton addTarget:self action:@selector(submitClick) forControlEvents:UIControlEventTouchUpInside];
  [self.view addSubview:submitButton];

  
  
}


- (void)backClick {
  [self.view endEditing: YES];
  [self goBackVC];
}

- (void)sendCode {
  
  if (_phoneField.text.length == 0) {
    [Tools showErrorWithStatus:YTLocalizedString(@"请输入手机号码")];
    return;
  }
  
  if (self.codeButton.selected == YES) {
    return;
  }
  [self.view endEditing:YES];
  __weak typeof(self) weakSelf = self;
  [_forgetServer sendmassgeCodeWithPhone:_phoneField.text finished:^(BOOL finish) {
    if (finish) {
      [SVProgressHUD showSuccessWithStatus:YTLocalizedString(@"验证码发送成功")];
    }
    [weakSelf countDownT];
  } failed:^(NSError *error) {
    
  }];
  
}

///! 提交
- (void)submitClick {
  if (_phoneField.text.length == 0) {
    [Tools showErrorWithStatus:YTLocalizedString(@"请输入手机号码")];
    return;
  } else if (_codeField.text.length == 0) {
    [Tools showErrorWithStatus:YTLocalizedString(@"请输入手机验证码")];
    return;
  } else if (_passwordField.text.length == 0) {
    [Tools showErrorWithStatus:YTLocalizedString(@"请输入密码")];
    return;
  }
  
  [self.view endEditing:YES];

  __weak typeof(self) weakSelf = self;
  [_forgetServer forgetPassword:_phoneField.text code:_codeField.text newPassword:_passwordField.text finished:^(BOOL finish) {
    if (finish) {
      [Tools showSuccessWithStatus:YTLocalizedString(@"修改成功")];
      [weakSelf goBackVC];
    }
    
  } failed:^(NSError *error) {
    
  }];
  
}

- (void)countDownT {
  dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  dispatch_block_t block = ^{
    int i = 60;
    while (i > 0) {
      if (i != 60) sleep(1);
      //耗时的操作
      NSLog(@"倒计时60秒----%d",i);
      i--;
      dispatch_async(dispatch_get_main_queue(), ^{
        if (self.codeButton) {
          if (i > 0) {
            // HEXCOLOR(0X888888)
            [self.codeButton setTitleColor:DEF_GRAY forState:UIControlStateNormal];
            [self.codeButton setTitle:[NSString stringWithFormat:@"%dS", i] forState:UIControlStateNormal];
            
            if (self.codeButton.selected == NO) {
              self.codeButton.selected = YES;
            }
            
          } else {
            [self.codeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            self.codeButton.selected = NO;
            [self.codeButton setTitle:YTLocalizedString(@"获取验证码") forState:UIControlStateNormal];
          }
        }
      });
    }
  };
  dispatch_async(queue, block);
}


@end
